package com.freight.tws.controller;

import java.util.List;
import java.io.IOException;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import lombok.RequiredArgsConstructor;
import com.freight.common.security.annotation.PreAuthorize;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.freight.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.freight.common.log.annotation.Log;
import com.freight.common.log.enums.BusinessType;
import com.freight.common.core.web.controller.BaseController;
import com.freight.common.core.web.domain.AjaxResult;
import com.freight.common.core.utils.poi.ExcelUtil;
import com.freight.common.core.web.page.Query;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.freight.common.core.web.page.Condition;
import com.freight.tws.domain.OrderAddress;
import com.freight.tws.service.IOrderAddressService;
import com.freight.common.core.web.page.TableDataInfo;

/**
 * 订单常用发/收货方地址Controller
 * 
 * @author ruoyi
 * @date 2023-05-05
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/orderAddress")
public class OrderAddressController extends BaseController
{
    private final IOrderAddressService iOrderAddressService;

    /**
     * 查询订单常用发/收货方地址列表
     */
    @PreAuthorize(hasPermi="tws:orderAddress:list")
    @GetMapping("/list")
    public TableDataInfo list(Query query, OrderAddress orderAddress)
    {
        LambdaQueryWrapper<OrderAddress> lqw = new LambdaQueryWrapper<OrderAddress>();
        if (StringUtils.isNotBlank(orderAddress.getType())) {
            lqw.eq(OrderAddress::getType, orderAddress.getType());
        }
        if (StringUtils.isNotBlank(orderAddress.getName())) {
            lqw.like(OrderAddress::getName ,orderAddress.getName());
        }
        if (StringUtils.isNotBlank(orderAddress.getIdCard())) {
            lqw.eq(OrderAddress::getIdCard, orderAddress.getIdCard());
        }
        if (StringUtils.isNotBlank(orderAddress.getLinkTel())) {
            lqw.eq(OrderAddress::getLinkTel, orderAddress.getLinkTel());
        }
        if (StringUtils.isNotBlank(orderAddress.getAddress())) {
            lqw.eq(OrderAddress::getAddress, orderAddress.getAddress());
        }
        if (StringUtils.isNotBlank(orderAddress.getCityCode())) {
            lqw.eq(OrderAddress::getCityCode, orderAddress.getCityCode());
        }
        if (StringUtils.isNotBlank(orderAddress.getLongitude())) {
            lqw.eq(OrderAddress::getLongitude, orderAddress.getLongitude());
        }
        if (StringUtils.isNotBlank(orderAddress.getLatitude())) {
            lqw.eq(OrderAddress::getLatitude, orderAddress.getLatitude());
        }
        if (StringUtils.isNotBlank(orderAddress.getTenantId())) {
            lqw.eq(OrderAddress::getTenantId, orderAddress.getTenantId());
        }
        lqw.orderByDesc(OrderAddress::getCreateTime);
        IPage<OrderAddress> list = iOrderAddressService.page(Condition.getPage(query), lqw);
        return getDataTable(list);
    }

    /**
     * 导出订单常用发/收货方地址列表
     */
    @PreAuthorize(hasPermi = "tws:orderAddress:export")
    @Log(title = "订单常用发/收货方地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderAddress orderAddress) throws IOException
    {
        LambdaQueryWrapper<OrderAddress> lqw = new LambdaQueryWrapper<OrderAddress>(orderAddress);
        List<OrderAddress> list = iOrderAddressService.list(lqw);
        ExcelUtil<OrderAddress> util = new ExcelUtil<OrderAddress>(OrderAddress. class);
        util.exportExcel(response, list, "orderAddress");
    }

    /**
     * 获取订单常用发/收货方地址详细信息
     */
    @PreAuthorize(hasPermi="tws:orderAddress:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iOrderAddressService.getById(id));
    }

    /**
     * 新增订单常用发/收货方地址
     */
    @PreAuthorize(hasPermi="tws:orderAddress:add")
    @Log(title = "订单常用发/收货方地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderAddress orderAddress) {
        return toAjax(iOrderAddressService.save(orderAddress) ? 1 : 0);
    }

    /**
     * 修改订单常用发/收货方地址
     */
    @PreAuthorize(hasPermi="tws:orderAddress:edit")
    @Log(title = "订单常用发/收货方地址" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderAddress orderAddress) {
        return toAjax(iOrderAddressService.updateById(orderAddress) ? 1 : 0);
    }

    /**
     * 删除订单常用发/收货方地址
     */
    @PreAuthorize(hasPermi="tws:orderAddress:remove")
    @Log(title = "订单常用发/收货方地址", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iOrderAddressService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

}
